﻿using System;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace SoftwareCenter
{
    public partial class Form1 : Form
    {
        CommandCenter c;
        private System.Threading.Timer timer;
        private SmsCenter s;
      
        private int intervalSeconds = 60;//XX秒检查一次程序启动状态
        private int tcpIntervalSeconds = 120;//XX秒检查一次网络连接状态
        private int counterInterval = 8;
        private int counter = 0;  //
        public Form1()
        {
            
            InitializeComponent();
            c = new CommandCenter();
            this.Load += new EventHandler(Form1_Load);
            this.counterInterval = (int)(tcpIntervalSeconds / intervalSeconds);
        }



        private void checkProcess(object sender)
        {
            counter++;
            if (counter >= counterInterval)
            {
                if (CheckNetWorkConnection.ReStartCheck())
                {
                    c.Restart();
                }
                counter = 0;
            }
            if (!c.IsProcessRunning("自动化"))
            {
                c.StartProcess("自动化");
            }
        }

        void Form1_Load(object sender, EventArgs e)
        {
            s = new SmsCenter();
            c.StartProcess("自动化");
            this.timer = new System.Threading.Timer(new TimerCallback(checkProcess), null, 1000 * intervalSeconds, 1000 * intervalSeconds);  
            counter = 0;    
        }
    }

    public class CheckNetWorkConnection
    {
        private static DateTime[] timeArray = { DateTime.Parse("2012-1-1 06:00:00"), DateTime.Parse("2012-1-1 20:00:00") };

        //是否该重启设备
        public static bool ReStartCheck()
        {
            var time = DateTime.Now;
            //暂定两个策略,定点点定时重启,
            //以及每5分钟检查网络连接,发生故障重启
            foreach (var s in timeArray)
            {
                var span = time - s;
                if (Math.Abs(span.TotalMinutes) <= 5)
                {
                    return true;
                }
            }
            if (!IsConnectionOn())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static bool IsConnectionOn()
        {
            try
            {
                var path = @"\Program Files\tpsautosurvey_pda_version_\";
                path = path + "netlog.txt";
                using (StreamReader sr = new StreamReader(path))
                {
                    var time = Convert.ToDateTime(sr.ReadLine());
                    sr.Close();
                    var span = DateTime.Now - time;
                    //当收到数据时,该文件会保存最新的时间(在TPSAutoSurvey程序中完成)
                    //2分钟内没收到服务器心跳,应该重启
                    if (span.TotalMinutes > 2)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch
            {
                return true;
            }
        }
    }
}